package app.model_package;
import java.util.ArrayList;

import app.PN.*;

public class ProtocolFactory {
	
	
	public static PetriNet createPN(LogModel input){
		
		ArrayList<String> places = new ArrayList<String>();
		ArrayList<String> transitions = new ArrayList<String>();
		
		AlphaAlgorithmForProtocol alphaProcess = new AlphaAlgorithmForProtocol(input);
        alphaProcess.getInitialPerformatives();

        alphaProcess.getFinalPerformatives();

        alphaProcess.buildSuccessionRelationsList();

        alphaProcess.buildCausalityRelationsList();

        alphaProcess.buildCausalityRelationSetsList();

        alphaProcess.buildPlacesList();
        
        alphaProcess.getPerformatives();
        transitions = alphaProcess.getListPerformatives();
        
        ArrayList<Place> pl = alphaProcess.getPlacesList();
        
        for (int i = 0 ; i<pl.size();i++){
        	places.add(pl.get(i).getId_place());
        }
        
        PetriNet aux = new PetriNet(places,transitions);
        ArrayList<Integer> marking = new ArrayList<Integer>();
        for(int i=0;i<alphaProcess.getListInitialPerformatives().size();i++){
        	aux.setIncomingEdgeTransition(alphaProcess.getListInitialPerformatives().get(i), pl.get(0).getId_place(), 1);
        }
        
        for(int i=0;i<alphaProcess.getListFinalPerformatives().size();i++){
        	aux.setOutcomingEdgeTransition(alphaProcess.getListFinalPerformatives().get(i), pl.get(pl.size()-1).getId_place(), 1);
        }
        
        for (int i = 0; i<pl.size();i++){
        	
        	ArrayList<String> from = pl.get(i).getFrom();
        	ArrayList<String> to = pl.get(i).getTo();
        	
        	for (int j = 0; j<from.size();j++){
        		aux.setOutcomingEdgeTransition(from.get(j), pl.get(i).getId_place(), 1);
        	}
        	
        	for (int j = 0; j<to.size();j++){
        		aux.setIncomingEdgeTransition(to.get(j), pl.get(i).getId_place(), 1);
        	}
        	
        	if (i==0)
        		marking.add(1);
        	else 
        		marking.add(0);
        
        }
        aux.setInitialMarking(marking);
    	return aux;
		
	}

}
